﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SACM.Common.Constantes;
using SACM.Utils.Extensions;
using System.Data;
using System.Web.Services;

namespace SACM.PagePaciente
{
    public partial class CitaPaciente : System.Web.UI.Page
    {
        #region Eventos
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Cambiar por la primera fecha disponible
                calen.SelectedDate = DateTime.Now.Date;
                dpFecha.Value = calen.SelectedDate;
                int idEmpresa = SACM.Common.Util.Convert.ToInt(Request["IdEmpresa"] );
                int idSucursal = SACM.Common.Util.Convert.ToInt(Request["IdSucursal"]);
                if (idEmpresa == 0) idEmpresa = 1;
                if (idSucursal == 0) idSucursal = 1;
                hdnIdEmpresa.Value = idEmpresa.ToString();
                hdnIdSucursal.Value = idSucursal.ToString();
                cmbDoc.ObtenDoctores(idEmpresa, idSucursal);
            }
        }

        protected void Calen_DayRender(object sender, DayRenderEventArgs e)
        {
            //if (e.Day.Date < DateTime.Now.Date || e.Day.Date > new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month)).Date.AddMonths(1).AddDays(1))
            //{
            //    e.Day.IsSelectable = false;
            //    e.Cell.BackColor = Color.FromArgb(216, 216, 218);
            //}
            //else
            //{
            //    e.Cell.CssClass = "dalta";
            //    //e.Cell.Text = "<a href='#' onclick=\"return selectDay(" + e.Day.DayNumberText + ")\">" + e.Day.DayNumberText + "</a>";
            //    //e.Cell.Attributes.Add("href", "alert('Hola'); return false;");
            //    //e.Cell.Text = string.Empty;
            //}

            if(Session["dsDias"] != null)
            {
                DataTable dt = ((DataSet) Session["dsDias"]).Tables[0];
                DataRow[] drs = dt.Select("Fecha=#" + e.Day.Date.Year.ToString() + "/" + e.Day.Date.Month.ToString("00") + "/" + e.Day.Date.Day.ToString("00") + "# and " +
                    "DiaExcluir=0");
                if (drs.Length > 0)
                {
                    e.Cell.CssClass = "dalta";
                }
                else
                {
                    e.Day.IsSelectable = false;
                    e.Cell.CssClass = "dNoCalen";//BackColor = Color.FromArgb(216, 216, 218);
                }

                DataTable dt2 = ((DataSet)Session["dsDias"]).Tables[1];
                drs = null;
                drs = dt2.Select("Fecha='" + e.Day.Date.Year.ToString() + "/" + e.Day.Date.Month.ToString("00") + "/" + e.Day.Date.Day.ToString("00") + "' and " +
                    "AltaDisp=1");
                if (drs.Length > 0)
                {
                    e.Cell.CssClass = "dalta";
                }

                DataTable dt3 = ((DataSet)Session["dsDias"]).Tables[1];
                drs = null;
                drs = dt3.Select("Fecha='" + e.Day.Date.Year.ToString() + "/" + e.Day.Date.Month.ToString("00") + "/" + e.Day.Date.Day.ToString("00") + "' and " +
                    "MedianaDisp=1");
                if (drs.Length > 0)
                {
                    e.Cell.CssClass = "dmedia";
                }

                DataTable dt4 = ((DataSet)Session["dsDias"]).Tables[1];
                drs = null;
                drs = dt4.Select("Fecha='" + e.Day.Date.Year.ToString() + "/" + e.Day.Date.Month.ToString("00") + "/" + e.Day.Date.Day.ToString("00") + "' and " +
                    "BajaDisp=1");
                if (drs.Length > 0)
                {
                    e.Cell.CssClass = "dbaja";
                }

                DataTable dt5 = ((DataSet)Session["dsDias"]).Tables[1];
                drs = null;
                drs = dt5.Select("Fecha='" + e.Day.Date.Year.ToString() + "/" + e.Day.Date.Month.ToString("00") + "/" + e.Day.Date.Day.ToString("00") + "' and " +
                    "SinDisp=1");
                if (drs.Length > 0)
                {
                    e.Cell.CssClass = "dSindisp";
                    e.Day.IsSelectable = false;
                }
            }
        }

        protected void cmbDoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDoc.SelectedValue != null)
            {
                getDias();
            }
        }

        protected void calen_SelectionChanged(object sender, EventArgs e)
        {
            dpFecha.Value = calen.SelectedDate;
        }
        #endregion

        #region Metodos
        private void getDias()
        {
            ArrayList parametros = new ArrayList();
            ConsultaServiceReference.ConsultaServiceClient servicio = new ConsultaServiceReference.ConsultaServiceClient();
            
            int idEmpresa = SACM.Common.Util.Convert.ToInt(Request["IdEmpresa"]);
            int idSucursal = SACM.Common.Util.Convert.ToInt(Request["IdSucursal"]);
            if (idEmpresa == 0) idEmpresa = 1;
            if (idSucursal == 0) idSucursal = 1;

            //Parámetros
            parametros.Add(CreaParametroConsulta("@pnIdEmpresa", idEmpresa, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdSucursal", idSucursal, ConsultaServiceReference.Parameter.vEnumType.Int));
            parametros.Add(CreaParametroConsulta("@pnIdDoctor", cmbDoc.SelectedValue, ConsultaServiceReference.Parameter.vEnumType.Int));
            ConsultaServiceReference.Parameter[] prms = new ConsultaServiceReference.Parameter[parametros.Count];
            parametros.CopyTo(prms);

            //Ejecuta consulta
            DataSet ds = servicio.EjecutaConsultaConParametros(General.SACM + ".spObtenDiasAgenda", prms);
            Session["dsDias"] = ds;
        }

        private static ConsultaServiceReference.Parameter CreaParametroConsulta(string _nombreParametro, object _valor, ConsultaServiceReference.Parameter.vEnumType _type)
        {
            ConsultaServiceReference.Parameter p = new ConsultaServiceReference.Parameter();
            p.nombreParametro = _nombreParametro;
            p.valor = _valor;
            p.type = _type;

            return p;
        }

        #endregion

        #region WebMethods
        [WebMethod]
        public static string EnviaCita(CitaServiceReference.Cita obj)
        {
            CitaServiceReference.CitaServiceClient srv = new CitaServiceReference.CitaServiceClient();

            srv.EnviaCita(obj);
            
            return obj.IdSolicitudCita;
        }
        #endregion
    }
}